package com.heima.Serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.Mapper.WemediaUserMapper;
import com.heima.Serivce.ApUserClient;
import com.heima.Serivce.ArticleCilent;
import com.heima.model.Article.Pojo.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojo.ApUser;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.MD5Utils;
import com.heima.utils.common.SaltUtil;
//import io.seata.spring.annotation.GlobalTransactional;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Log4j
@Service
@Transactional
public class WemediaService implements com.heima.Serivce.WemediaService {
    @Autowired
    WemediaUserMapper wemediaUserMapper;

    @Qualifier("com.heima.Serivce.ApUserClient")
    @Autowired
    ApUserClient apUserClient;

    @Qualifier("com.heima.Serivce.ArticleCilent")
    @Autowired
    ArticleCilent articleCilent;

    //    登录
    @Override
    public ResponseResult Login(WmUser wmUser) {

        QueryWrapper<WmUser> wrapper = new QueryWrapper<>();
        wrapper.eq("name", wmUser.getName());
        WmUser tempUser = wemediaUserMapper.selectOne(wrapper);
//        判断用户是否存在
        if (tempUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,
                    "用户不存在");
        }
//         判断密码是否正确
        String password = MD5Utils.encodeWithSalt(wmUser.getPassword(), tempUser.getSalt());
        if (!tempUser.getPassword().equals(password)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
//        返回数据
        Map<String, Object> arr = new HashMap<>();
        arr.put("token", AppJwtUtil.getToken(tempUser.getId().longValue()));
        tempUser.setSalt("");
        tempUser.setPassword("");
        arr.put("user", tempUser);
        return ResponseResult.okResult(arr);
    }

    //    添加自媒体用户
    @Override
//    分布式事务管理的注解
    @GlobalTransactional
//    跨微服务调用mapper seata  feign
    public ResponseResult AddUser(WmUser wmUser) {
        int num = 0;
//        判断是否是普通用户
        ApUser apUser = apUserClient.GetUser(wmUser.getApUserId());
        if (apUser != null) {
            wmUser.setPhone(apUser.getPhone());
        }
        String Salt = SaltUtil.GetSalt();
        String password = wmUser.getPassword();
        wmUser.setPassword(MD5Utils.encodeWithSalt(password, Salt));
        wmUser.setSalt(Salt);
        wmUser.setCreatedTime(new Date());
        num = wemediaUserMapper.insert(wmUser);

//            为作者自动创建用户账号，用户名和密码相同
        if (apUser == null) {
            apUser.setName(wmUser.getName());
            apUser.setPassword(wmUser.getPassword());
            apUser.setPhone(wmUser.getPhone());
            apUser.setFlag((short) 1);
            apUserClient.AddUser(apUser);
        }

//        添加App端作者信息
        ApUser apUser1 = apUserClient.GetUser(wmUser.getApUserId());
        ApAuthor apAuthor = new ApAuthor();
        apAuthor.setUserID(apUser1.getId());
        apAuthor.setWmUserId(wmUser.getId());
        apAuthor.setName(wmUser.getName());
        apAuthor.setCreateTime(new Date());
        articleCilent.AddAuthor(apAuthor);

        return ResponseResult.okResult(num);
    }


}
